package org.yuchen.tag;

import java.lang.reflect.Method;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class GetProperty extends BodyTagSupport {
	private static final long serialVersionUID = -8962572781281555374L;
	private String name;
	private String property;

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doStartTag() throws JspException {

		try {
			Object obj = pageContext.findAttribute(name);

			if (obj == null) {
				return SKIP_BODY;
			}

			Class c = obj.getClass();
			// ����GET�������� get+������(�������һ����ĸ��д)
			String getMethodName = "get"
					+ property.substring(0, 1).toUpperCase()
					+ property.substring(1, property.length());
			Method getMethod = c.getMethod(getMethodName, new Class[] {});

			pageContext.getOut().print(getMethod.invoke(obj));
			System.out.print(property + ":" + getMethod.invoke(obj) + "\t");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setProperty(String property) {
		this.property = property;
	}

}
